package com.longcai.service.impl.homepage;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.entity.homegood.ClyjGoodsBrand;
import com.longcai.entity.homegood.ClyjGoodsHomepage;
import com.longcai.entity.homepage.ClyjGoodsType;
import com.longcai.entity.homepage.GoodsClass;
import com.longcai.mapper.homegood.ClyjGoodsBrandMapper;
import com.longcai.mapper.homegood.ClyjGoodsHomepageMapper;
import com.longcai.mapper.homepage.ClyjGoodsTypeMapper;
import com.longcai.mapper.homepage.GoodsClassMapper;
import com.longcai.service.homepage.ClyjUserSearchService;
import com.longcai.service.homepage.GoodsClassService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品类型 服务实现类
 * </p>
 *
 * @author LWK
 * @since 2019-03-20
 */
@Service
public class GoodsClassServiceImpl extends ServiceImpl<GoodsClassMapper, GoodsClass> implements GoodsClassService {
    @Autowired
    GoodsClassMapper classMapper;
    @Autowired
    ClyjGoodsBrandMapper brandMapper;
    @Autowired
    ClyjGoodsTypeMapper clyjGoodsTypeMapper;
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjGoodsHomepageMapper clyjGoodsHomepageMapper;
    @Autowired
    private ClyjUserSearchService clyjUserSearchService;

    /**
     * description: 根据一级分类获取品牌以及所有分类
     * author: LWK
     * time： 2019年3月22日10点10分
     */
    @Override
    public Map<String, Object> getAllClassList(JSONObject params) {
        Map<String, Object> result = new HashMap<String, Object>();
        String classId = params.getString("class_id");
        //查询二级分类
        QueryWrapper<GoodsClass> queryWrapper = new QueryWrapper<GoodsClass>();
        //分类类型（0：活动分类；1：商品分类）
        queryWrapper.eq("type", "1");
        //分类状态（0：正常；1：禁用；2：删除）
        queryWrapper.eq("type_status", "0");
        queryWrapper.eq("p_id", classId);
        //排序字段
        queryWrapper.orderByAsc("type_sort");
        List<GoodsClass> selectList = classMapper.selectList(queryWrapper);
        //使用map存储分类
        List<Map<String, Object>> classList = new ArrayList<Map<String, Object>>();
        for (GoodsClass goodsClass : selectList) {
            Map<String, Object> classMap = new HashMap<String, Object>();
            Class<? extends GoodsClass> class1 = goodsClass.getClass();
            Field[] fields = class1.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    classMap.put(field.getName(), field.get(goodsClass));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            //查询三级分类并存储到map中
            QueryWrapper<GoodsClass> queryWrapper2 = new QueryWrapper<GoodsClass>();
            //分类类型（0：活动分类；1：商品分类）
            queryWrapper2.eq("type", "1");
            //分类状态（0：正常；1：禁用；2：删除）
            queryWrapper2.eq("type_status", "0");
            //排序字段
            queryWrapper2.orderByAsc("type_sort");
            queryWrapper2.eq("p_id", goodsClass.getId());
            List<GoodsClass> selectList2 = classMapper.selectList(queryWrapper2);
            classMap.put("subList", selectList2);
            classList.add(classMap);
        }
        result.put("goodsClass", classList);
        //查询分类下的品牌
        List<ClyjGoodsBrand> brandList = brandMapper.getBrandsByClassId(classId);
        result.put("goodsBrand", brandList);
        return result;
    }

    /**
     * 热销商品
     *
     * @param id
     * @param params
     * @return
     */
    @Override
    public ApiResult getTopgoods(String id, JSONObject params) {
        String type = params.getString("type");
        Map map = new HashMap();
        Map map1 = new HashMap();
        Map map2 = new HashMap();
        List list = new ArrayList();
        List list1 = new ArrayList();
        try {
            QueryWrapper<ClyjGoodsType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type_id", id);
            List<ClyjGoodsType> clyjGoodsTypes = this.clyjGoodsTypeMapper.selectList(queryWrapper);
            //如果一级分类为空 查全部热销商品
            if (type == null || type.equals("")) {
                for (ClyjGoodsType clyjGoodsType : clyjGoodsTypes) {
                    list.add(clyjGoodsType.getGoodsId());
                }
                map.put("clyjGoodsTypes", list);
                List<ClyjGoodsHomepage> clyjGoodsHomepages = this.clyjGoodsHomepageMapper.selectallgoods(map);
                logger.debug("查询全部成功");
                return ApiResult.success(clyjGoodsHomepages);
            }
            QueryWrapper<ClyjGoodsType> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("type_id", type);
            //去查级一级分类对应商品id
            List<ClyjGoodsType> clyjGoodsTypeList1 = this.clyjGoodsTypeMapper.selectList(queryWrapper1);
            for (ClyjGoodsType clyjGoodsType : clyjGoodsTypeList1) {
                list1.add(clyjGoodsType.getGoodsId());
            }
            map1.put("list1", list1);
            map1.put("id", id);
            //查询符合两个条件的商品id
            List list2 = new ArrayList();
            List<ClyjGoodsType> clyjGoodsTypeList = this.clyjGoodsTypeMapper.selecttype(map1);
            for (ClyjGoodsType clyjGoodsType : clyjGoodsTypeList) {
                list2.add(clyjGoodsType.getGoodsId());
            }
            map2.put("list2", list2);
            List<ClyjGoodsHomepage> clyjGoodsHomepages = this.clyjGoodsHomepageMapper.select(map2);
            logger.debug("查询成功++++++", clyjGoodsHomepages);
            return ApiResult.success(clyjGoodsHomepages);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail("查询失败" + e.getMessage());
        }
    }

    /**
     * 获取首页商品的所有二级分类
     *
     * @return
     */
    @Override
    public ApiResult getgoodsclass() {
        try {
            QueryWrapper<GoodsClass> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type", "1");
            queryWrapper.isNull("p_id");
            List<GoodsClass> goodsClassList = this.classMapper.selectList(queryWrapper);
            logger.debug("查询成功+++++++++", goodsClassList);
            return ApiResult.success(goodsClassList);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail("查询失败" + e.getMessage());
        }
    }

    /**
     * 好物推荐
     *
     * @param id
     * @return
     */
    @Override
    public ApiResult getrecommendgoods(String id) {
        List list = new ArrayList();
        Map map = new HashMap();
        try {
            QueryWrapper<ClyjGoodsType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("type_id", id);
            List<ClyjGoodsType> clyjGoodsTypes = this.clyjGoodsTypeMapper.selectList(queryWrapper);
            for (ClyjGoodsType clyjGoodsType : clyjGoodsTypes) {
                list.add(clyjGoodsType.getGoodsId());
            }
            map.put("clyjGoodsTypes", list);
            List<ClyjGoodsHomepage> clyjGoodsHomepages = this.clyjGoodsHomepageMapper.selectallgoods(map);
            logger.debug("查询全部成功");
            return ApiResult.success(clyjGoodsHomepages);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail("查询失败" + e.getMessage());
        }
    }


    /**
     * 附近全部分类
     *
     * @return
     */
    @Override
    public Map<String, Object> getNearMenuList() {
        Map<String, Object> result = new HashMap();
        //查询一级分类
        QueryWrapper<GoodsClass> queryWrapper = new QueryWrapper<GoodsClass>();
        queryWrapper.eq("type", "2");
        //分类状态（0：正常；1：禁用；2：删除）
        queryWrapper.eq("type_status", "0");
        queryWrapper.isNull("p_id");
        //排序字段
        //queryWrapper.orderByAsc("type_sort");
        List<GoodsClass> selectList = this.classMapper.selectList(queryWrapper);
        //使用map存储分类
        List<Map<String, Object>> classList = new ArrayList<Map<String, Object>>();
        for (GoodsClass goodsClass : selectList) {
            Map<String, Object> classMap = new HashMap<String, Object>();
            Class<? extends GoodsClass> class1 = goodsClass.getClass();
            Field[] fields = class1.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                try {
                    classMap.put(field.getName(), field.get(goodsClass));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            //查询二级分类并存储到map中
            QueryWrapper<GoodsClass> queryWrapper2 = new QueryWrapper<GoodsClass>();
            queryWrapper2.eq("type", "2");
            //分类状态（0：正常；1：禁用；2：删除）
            queryWrapper2.eq("type_status", "0");
            //排序字段
            queryWrapper2.orderByAsc("type_sort");
            queryWrapper2.eq("p_id", goodsClass.getId());
            List<GoodsClass> selectList2 = classMapper.selectList(queryWrapper2);
            classMap.put("subList", selectList2);
            classList.add(classMap);
            result.put("goodsClass", classList);
        }
        return result;
    }
}
